package cn.hejiang.mamis.core.controller;

import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;

import cn.hejiang.mamis.core.dto.AmisCrudDTO;
import cn.hejiang.mamis.core.dto.PlatUserDTO;
import cn.hejiang.mamis.core.dto.ResultDTO;
import cn.hejiang.mamis.core.entity.PlatUser;
import cn.hejiang.mamis.core.entity.SessionDetails;
import cn.hejiang.mamis.core.service.ContextService;
import cn.hejiang.mamis.core.service.PlatUserService;
import cn.hejiang.mamis.core.util.QueryUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import lombok.extern.slf4j.Slf4j;

@RequestMapping("/mamis/plat/user")
@RestController
@Slf4j
public class PlatUserController {
	@Autowired
	ContextService contextService;

	@Autowired
	PlatUserService platUserService;

	@GetMapping("/list")
	public ResultDTO<AmisCrudDTO<PlatUserDTO>> getUserList(final HttpServletRequest request) {
		// 查询数据库
		List<PlatUserDTO> list = platUserService.getUserList(contextService.getSystem().getId());

		// 构造 amis 要求的数据结构
		final AmisCrudDTO<PlatUserDTO> data = new AmisCrudDTO<>();
		data.setItems(list);
		//data.setTotal(platUserMapper.selectCount(wrapper));

		// 返回结果集
		return ResultDTO.success(data);
	}

	@PostMapping()
	public ResultDTO saveUser(final HttpServletRequest request, @RequestBody final PlatUser obj) {
		final SessionDetails session = contextService.getSession(request);

		// 清除不可更新字段
		obj.setDeleted(null);
		obj.setOwnSysId(null);
		obj.setOwnDeptId(null);
		obj.setCreatedAt(null);
		obj.setCreatedBy(null);
		obj.setUpdatedAt(null);
		obj.setUpdatedBy(session.getCurUserId());

		boolean ret;
		if (StrUtil.isNotBlank(obj.getId())) {
			// update
			UpdateWrapper<PlatUser> updater = QueryUtil.newUpdateWrapper(obj.getId());
			ret = platUserService.update(obj, updater);
		} else {
			// insert
			obj.setId(null);
			obj.setCreatedBy(session.getCurUserId());
			obj.setOwnSysId(contextService.getSystem().getId());
			ret = platUserService.save(obj);
		}

		return ret ? ResultDTO.success() : ResultDTO.error("保存失败");
	}

	@DeleteMapping("/{id}")
	public ResultDTO delUser(final HttpServletRequest request, @PathVariable("id") String id) {
		UpdateWrapper<PlatUser> updater = QueryUtil.newDeleteWrapper(id);
		boolean ret = platUserService.update(null, updater);

		return ret ? ResultDTO.success() : ResultDTO.error("删除失败");
	}

	@PostMapping("/password")
	public ResultDTO setUserPassword(final HttpServletRequest request, @RequestBody final PlatUser obj) {
		final PlatUser user = platUserService.getUserById(obj.getId());

		String salt = user.getSalt();
		if (StrUtil.isBlank(salt)) {
			salt = UUID.randomUUID().toString();
		}

		final String password = SecureUtil.md5(obj.getPassword() + salt);

		final PlatUser entity = new PlatUser();
		entity.setId(obj.getId());
		entity.setPassword(password);
		entity.setSalt(salt);

		boolean ret = platUserService.updateById(entity);

		return ret ? ResultDTO.success("重置密码后用户需要重新登录") : ResultDTO.error("修改密码失败");
	}

}
